use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // Create sys_user_role table
        manager
            .create_table(
                Table::create()
                    .table(SysUserRole::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysUserRole::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysUserRole::UserId).big_integer().not_null())
                    .col(ColumnDef::new(SysUserRole::RoleId).big_integer().not_null())
                    .col(ColumnDef::new(SysUserRole::Creator).big_integer())
                    .col(ColumnDef::new(SysUserRole::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysUserRole::Updater).big_integer())
                    .col(ColumnDef::new(SysUserRole::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysUserRole::Version).integer().default(0))
                    .col(ColumnDef::new(SysUserRole::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        // Create sys_role_menu table
        manager
            .create_table(
                Table::create()
                    .table(SysRoleMenu::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(SysRoleMenu::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(SysRoleMenu::RoleId).big_integer().not_null())
                    .col(ColumnDef::new(SysRoleMenu::MenuId).big_integer().not_null())
                    .col(ColumnDef::new(SysRoleMenu::Creator).big_integer())
                    .col(ColumnDef::new(SysRoleMenu::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(SysRoleMenu::Updater).big_integer())
                    .col(ColumnDef::new(SysRoleMenu::UpdatedAt).timestamp_with_time_zone())
                    .col(ColumnDef::new(SysRoleMenu::Version).integer().default(0))
                    .col(ColumnDef::new(SysRoleMenu::Deleted).integer().default(0))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysRoleMenu::Table).to_owned())
            .await?;
        
        manager
            .drop_table(Table::drop().table(SysUserRole::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum SysUserRole {
    Table,
    Id,
    UserId,
    RoleId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}

#[derive(DeriveIden)]
enum SysRoleMenu {
    Table,
    Id,
    RoleId,
    MenuId,
    Creator,
    CreatedAt,
    Updater,
    UpdatedAt,
    Version,
    Deleted,
}